using System;
using System.Configuration;

namespace Irc.Net
{
	/// <summary>
	/// Description of SettingsManager.
	/// </summary>
	public sealed class SettingsManager : IDisposable
	{
		private static SettingsManager instance = new SettingsManager();
		public static SettingsManager Instance {
			get {
				return instance;
			}
		}
		public bool SaveOnExit
		{
			get { return Convert.ToBoolean(this["SaveOnExit"]); }
			set { this["SaveOnExit"] = value.ToString(); }
		}
		public System.Drawing.Point Location
		{
			get {
				string[] values = this["Location"].Split('|');
				return new System.Drawing.Point(Convert.ToInt32(values[0]), Convert.ToInt32(values[1]));
			}
			set {
				this["Location"] = value.X + "|" + value.Y;
			}
		}
		public bool UseTimestamps
		{
			get { return Convert.ToBoolean(this["UseTimestamps"]); }
			set { this["UseTimestamps"] = value.ToString(); }
		}
		public bool DebugMessages
		{
			get { return Convert.ToBoolean(this["DebugMessages"]); }
			set { this["DebugMessages"] = value.ToString(); }
		}
		public bool AutofocusChannels
		{
			get { return Convert.ToBoolean(this["AutofocusChannels"]); }
			set { this["AutofocusChannels"] = value.ToString(); }
		}
		public bool AutofocusMessages
		{
			get { return Convert.ToBoolean(this["AutofocusMessages"]); }
			set { this["AutofocusMessages"] = value.ToString(); }
		}
		public bool ConfirmClose
		{
			get { return Convert.ToBoolean(this["ConfirmClose"]); }
			set { this["ConfirmClose"] = value.ToString(); }
		}

		private SettingsManager(){}
		[System.Runtime.CompilerServices.IndexerName("Properties")]
		public string this[string index]
		{
			get {
				string value = ConfigurationManager.AppSettings.Get(index);
				if(value == null)
					throw new ArgumentNullException(value);
				return value.Split(' ')[0];
			}
			set {
				ConfigurationManager.AppSettings.Set(index, value);
			}
		}
		public void Dispose()
		{
			if(!SaveOnExit) return;
			try
			{
				Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
				config.Save(ConfigurationSaveMode.Modified);
			} finally {}
		}
	}
}
